package com.web.boot.web.cms;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.http.message.ViewBuilder;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.boot.global.model.User;
import com.web.boot.global.util.HttpContext;
import com.web.cms.doc.facade.ChannelFacade;
import com.web.cms.doc.facade.DocFacade;
import com.web.cms.doc.message.DocRequest;
import com.web.cms.doc.message.DocResponse;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

@Controller("cmsDocController")
@Http(url = "/cms")
public class DocController
{
	@Autowired
	private DocFacade docFacade;
	@Autowired
	private ChannelFacade channelFacade;
	@Resource
	private HttpContext httpContext;
	
	private DictParser dictParser = DictParser.getInstance();
	
	public DocController()
	{
	}
	
	@Http(url = "/doc_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		DocRequest docRequest = request.getQueryObject(DocRequest.class);
		ViewBuilder builder = View.createBuilder();
		if (docRequest.getChannelId() != null)
		{
			builder.put("parentModel", channelFacade.get(docRequest.getChannelId()));
		}
		return builder.setView("/cms/doc_list")
				.put("model", docRequest)
				.put("map", dictParser.getStringMap())
				.put("page", docFacade.listPage(docRequest, Const.PAGE_START, Const.PAGE_SIZE))
				.build();
	}
	
	@Http(url = "/doc_list", method = HttpMethod.POST)
	public View listPost(HttpRequest request)
	{
		DocRequest docRequest = request.getPostObject(DocRequest.class);
		int start = request.getPostInt("start", Const.PAGE_START);
		return View.createBuilder().setView("/cms/doc_list_grid")
				.put("page", docFacade.listPage(docRequest, start, Const.PAGE_SIZE))
				.build();
	}
	
	@Http(url = "/doc_get", method = HttpMethod.GET)
	public View get(HttpRequest request)
	{
		DocRequest docRequest = request.getQueryObject(DocRequest.class);
		DocResponse response = null;
		if (docRequest.getId() != null)
		{
			response = docFacade.get(docRequest.getId());
			docRequest.setChannelId(response.getChannelId());
		}
		ViewBuilder builder = View.createBuilder();
		if (docRequest.getChannelId() != null)
		{
			builder.put("parentModel", channelFacade.get(docRequest.getChannelId()));
		}
		if (response == null)
		{
			response = new DocResponse();
			User user = httpContext.getUser(request);
			response.setUserId(user.getId());
			response.setAuthor(user.getNickname());
		}

		return builder.setView("/cms/doc_get")
				.put("model", response)
				.put("map", dictParser.getStringMap())
				.build();
	}
	
	@Http(url = "/doc_save", method = HttpMethod.POST)
	public void save(HttpRequest request)
	{
		DocRequest docRequest = request.getPostObject(DocRequest.class);
		User user = httpContext.getUser(request);
		docRequest.setUserId(user.getId());
		docRequest.setAuthor(user.getNickname());
		
		if (docRequest.getId() == null)
		{
			ValidationUtil.valid(docRequest, SaveModel.class);
			docFacade.add(docRequest);
		}
		else
		{
			ValidationUtil.valid(docRequest, UpdateModel.class);
			docFacade.update(docRequest);
		}
	}
	
}
